/**
 * Created by jiabin.yu on 2018/9/22.
 */
import { Template } from 'meteor/templating';
import '/lib/collection.js';

Template.Result.onCreated(function(){
    var template= Template.instance();
    template.A = new ReactiveVar(false);
});
Template.Result.helpers({
    logined:function () {

        var adm = JSON.parse(localStorage.getItem('key'));

        if(adm){
            return true
        }else {
            return false
        }

    },
    tableSettings: function () {
        var UserTable=Userinfo.find();
        if(UserTable.count()){
            return {
                collection:UserTable,
                rowsPerPage: 5,
                showFilter: true,
                fields: [
                    {
                        key:'username',
                        label:'姓名',
                        fn:function (value,obj,key) {
                            return  new Spacebars.SafeString(value)
                        },
                        sortable: false
                    },
                    {
                        key:'school',
                        label:'学校',
                        fn:function (value,obj,key) {
                            return  new Spacebars.SafeString(value)
                        },
                        sortable: false
                    },
                    {
                        key:'phone',
                        label:'手机号',
                        fn:function (value,obj,key) {
                            return  new Spacebars.SafeString(value)
                        },
                        sortable: false
                    },
                    {
                        key:'num',
                        label:'报名编号',
                        fn:function (value,obj,key) {
                            return  new Spacebars.SafeString(value)
                        },
                        sortable: false
                    },
                    {
                        key:'time',
                        label:'时间',
                        fn:function (value,obj,key) {
                            return  new Spacebars.SafeString(value.toLocaleDateString())
                        },
                        sortable: false
                    }
                ],
            }
        }else {
            return []
        }
    }
});

Template.Result.events({
    "click #Login":function (e,t) {
        var admin=$("#admin").val();
        var pass=$("#pass").val();
        console.log(Template.instance())
        Meteor.call('session_validate',{
            Admin:admin,
            Pass:pass
        },(err,result)=>{
            if(!err){
                console.log(result);
                if(result){
                    var info = {admin: admin, pass: pass};
                    localStorage.setItem('key', JSON.stringify(info));
                    window.location.reload()
                }else {
                    alert("用户名或密码错误")
                }
            }else {
                console.log(0);
            }
        })

    }
})
